package cn.avalon.basic.service.impl;

import cn.avalon.basic.query.BaseQuery;
import cn.avalon.basic.mapper.BaseMapper;
import cn.avalon.basic.service.BaseService;
import cn.avalon.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description: 业务实现类：部门
 * @author: Avalon
 * @date: 2022年08月21日 ： 19:56
 * @version: ver1.0
 * @email 709785494@qq.com
 */
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements BaseService<T> {

    //注入：从Spring容器中获取当前类或子类的对象  赋值 给当前属性
    @Autowired
    //IDEA以为Spring容器中没有该类型或其子类型的对象，又在使用，所以报错
    //它错了 - 因为已经通过在App中@MapperScan("cn.avalon.*.mapper")已经生成了
    private BaseMapper<T> baseMapper;

    @Override
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }

    @Override
    @Transactional
    public void remove(Long id) {
        baseMapper.remove(id);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public T findById(Long id) {
        return baseMapper.findById(id);
    }

    @Override
    public List<T> findAll() {
        return baseMapper.findAll();
    }

    @Override
    public PageList<T> queryPage(BaseQuery baseQuery) {
        // 1.查询总条数
        Integer totals = baseMapper.queryCount(baseQuery);
        if(totals==0){
            return new PageList<>();
        }
        // 2.查询当前页的分页数据
        List<T> data = baseMapper.queryData(baseQuery);
        return new PageList<>(data,totals);
    }

    @Override
    public void patchDelete(List<Long> ids) {
        baseMapper.patchDelete(ids);
    }
}
